package com.sinoglobal.dumping.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.sinoglobal.dumping.R;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/1/11.
 */
public class Dumpling_PhotosSeletorAdapter extends BaseAdapter {
    private static final int TYPE_CAMERA = 0;
    private static final int TYPE_NORMAL = 1;
    private Context mContext;
    private ArrayList<String> imgs;
    private GridView.LayoutParams viewLayoutParams;
    private int imageSize;

    public Dumpling_PhotosSeletorAdapter(Context context) {
        mContext = context;

    }

    public void setDataList(ArrayList<String> imgsPaths) {
        imgs = imgsPaths;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return imgs != null ? imgs.size() + 1 : 0;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        return position == 0 ? TYPE_CAMERA : TYPE_NORMAL;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    public void setItemSize(int itemSize) {
        imageSize = itemSize;
        viewLayoutParams = new GridView.LayoutParams(itemSize, itemSize);
        notifyDataSetChanged();
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (getItemViewType(i) == TYPE_CAMERA) {
            view = View.inflate(mContext, R.layout.dumpling_photos_seletor_camera_item, null);
            view.setTag(null);
        } else if (getItemViewType(i) == TYPE_NORMAL) {
            ViewHoder holder;
            if (view == null) {
                holder = new ViewHoder();
                view = View.inflate(mContext, R.layout.dumpling_photos_seletor_photo_item, null);
                holder.imageView = (ImageView) view.findViewById(R.id.dumpling_card_photo_seletor_img);
                view.setTag(holder);
            } else {
                holder = (ViewHoder) view.getTag();
                if (holder == null) {
                    holder = new ViewHoder();
                    view = View.inflate(mContext, R.layout.dumpling_photos_seletor_photo_item, null);
                    holder.imageView = (ImageView) view.findViewById(R.id.dumpling_card_photo_seletor_img);
                    view.setTag(holder);
                }
            }
            if (imageSize > 0) {
                Picasso.with(mContext)
                        .load("file://"+imgs.get(i - 1))
                        .placeholder(R.drawable.dumpling_default_avatar)
                        .resize(imageSize, imageSize)
                        .centerCrop()
                        .into(holder.imageView);
            }
        }
        view.setLayoutParams(viewLayoutParams);
        return view;
    }


    class ViewHoder {
        public ImageView imageView;
    }
}
